﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Galoap
{
    partial class PhraseQuestion : Question
    {
        /// <summary>
        /// The parser interrogator used for text analysis
        /// </summary>
        public ParserInterrogator pi = new MyParserInterrogator();
        
        /// <summary>
        /// The part of speech of the phrase
        /// </summary>
        public Ontology[] PartOfSpeech { get; set; }
        
        /// <summary>
        /// The various possible semantic tree for the phrase in Ontology
        /// </summary>
        public Ontology[][] TreeOntology { get; set; }
        
        /// <summary>
        /// The various possible semantic tree in Tree
        /// </summary>
        public Tree[] Tree { get; set; }

        /// <summary>
        /// The constructor
        /// </summary>
        public PhraseQuestion() : base()
        {
            threshold = 0; 
            probablyOfRequestOfConfirm = 0f;
        }

        /// <summary>
        /// This method save an answer (a serialized ontology list) to database, from a string indicating the index of the tree ontology table.
        /// </summary>
        /// <typeparam name="T">the type of the answer to save</typeparam>
        /// <param name="gs">the gamesession of the player who is answering to the question</param>
        /// <param name="answer">the answer given by the player (a number form 0 to 9)</param>
        /// <returns>the answer saved</returns>
        public override T SaveAnswer<T>(GameSession gs, string answer)
        {
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            bf.Serialize(stream, TreeOntology[System.Convert.ToInt32(answer, 10)]);

            byte[] b = stream.GetBuffer();
            stream.Close();

            string s = System.Convert.ToBase64String(b);

            return base.SaveAnswer<T>(gs, s);
        }

        /// <summary>
        /// This method initializes the question creating or setting the instance variables
        /// </summary>
        public override void Initialize()
        { 
            base.Initialize();
            if(Document.Count <= 0)
                Document.Load();
            pi.phrase = Document.First().ToString();
            PartOfSpeech = pi.PartOfSpeechRequest();
            Tree = pi.SemanticTreesRequest(10);
            TreeOntology = pi.SemanticOntologyTreesRequest(10);
        }

    }
}
